package controller.expertInfo.adminEdit;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import controller.common.BaseController;
import controller.common.StaticConstant;
import net.sf.json.JSONArray;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import response.CommonResult;
import services.dictionary.CertificateTypeServices;
import services.dictionary.MajorTypeServices;
import services.expertInfo.ExpertMajorServices;
import validator.NotBlankFluentValidator;
import wt.entity.dictionary.MajorType;
import wt.webPlugin.ZTree;

import java.util.List;
import java.util.Map;

;

/**
 * 功能:
 * 作者: 洪世成
 * 日期: 2017/9/9
 * 版权所有: admin
 */
@Controller

@RequestMapping("expertInfo/adminEdit/expertMajor")
public class ExpertMajorController extends BaseController {

    @Autowired
    ExpertMajorServices expertMajorServices;
    @Autowired
    CertificateTypeServices certificateTypeServices;
    @Autowired
    MajorTypeServices majorTypeServices;

    @RequiresPermissions("expertInfo:adminEdit:show")
    @RequestMapping("index")
    public String index(String expertId, Map map) throws Exception {
        List<MajorType> majorTypeList = expertMajorServices.adminSelectForShow(expertId);
        JSONArray expertMajorList = ZTree.majorType2ZTreeJSONArray(majorTypeList, false);
        map.put("data", expertMajorList);
        return "expertInfo/adminEdit/expertMajor/index";
    }


    @RequiresPermissions("expertInfo:adminEdit:edit")
    @RequestMapping("editDlg")
    public String editDlg(String expertId,Map map) throws Exception {

        List<MajorType> majorTypeList = majorTypeServices.selectMajorType(null, true);
        List<String>    majorTypeIdList = expertMajorServices.adminSelectCheckedLeafIds(expertId);

        for (MajorType majorType : majorTypeList) {
            majorType.setLive((short) 0);
            for (String id : majorTypeIdList) {
                if (id.equals(majorType.getId())) {
                    majorType.setLive((short) 1);
                    break;
                }
            }
        }

        map.put(StaticConstant.EDIT_DATA,  ZTree.majorType2ZTreeJSONArray(majorTypeList, true));
        map.put(StaticConstant.EDIT_URL, "expertInfo/adminEdit/expertEducation/insert");
        map.put(StaticConstant.EDIT_OPERATE_TYPE, StaticConstant.EDIT_INSERT);
        return "expertInfo/adminEdit/expertMajor/editDlg";
    }





    @RequiresPermissions("expertInfo:adminEdit:edit")
    @ResponseBody
    @RequestMapping("update")
    public synchronized CommonResult update(String expertId,
                               @RequestParam(value = "insertLeafIds[]", required = false) List<String> insertLeafIdList,
                               @RequestParam(value = "deleteLeafIds[]", required = false) List<String> deleteLeafIdList) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        ComplexResult result = FluentValidator.checkAll().failOver()
                .on(expertId, new NotBlankFluentValidator("专家id无效"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            commonResult.addMessage(result);
            return commonResult.setError();
        }
        expertMajorServices.adminUpdateMajor(insertLeafIdList, deleteLeafIdList, expertId);
        return commonResult.setSuccess();
    }


}
